package com.clever.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.clever.util.SpringUtil;
import com.clever.annotation.Auth;
import com.clever.annotation.AuthGroup;
import com.clever.bean.model.OnlineUser;
import com.clever.bean.model.Result;

import java.util.List;

import com.clever.bean.shopping.Logistics;
import com.clever.service.LogisticsService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 订单物流接口
 *
 * @Author xixi
 * @Date 2024-04-08 11:11:32
 */
@RestController
@Validated
@RequestMapping("/logistics")
@AuthGroup(value = "order-second-hand-transaction.logistics", name = "订单物流模块", description = "订单物流模块权限组")
public class LogisticsController {

    @Resource
    private LogisticsService logisticsService;


    /**
     * 分页查询订单物流列表
     *
     * @param pageNumber 页码
     * @param pageSize   每页记录数
     * @param orderId    订单id
     * @return 当前页数据
     */
    @GetMapping("/page/{pageNumber}/{pageSize}")
    @Auth(value = "order-second-hand-transaction.logistics.page", name = "订单物流分页", description = "订单物流分页接口")
    public Result
            <Page<Logistics>> selectPage(@PathVariable("pageNumber") Integer pageNumber, @PathVariable("pageSize") Integer pageSize, String orderId) {
        return new Result<>(logisticsService.selectPage(pageNumber, pageSize, orderId), "分页数据查询成功");
    }

    /**
     * 根据订单id获取列表
     *
     * @param orderId 订单id
     * @return List<Logistics> 订单物流列表
     */
    @GetMapping("/listByOrderId/{orderId}")
    @Auth(value = "order-second-hand-transaction.logistics.listByOrderId", name = "根据订单id获取订单物流列表", description = "根据订单id获取订单物流列表接口")
    public Result
            <List<Logistics>> selectListByOrderId(@PathVariable("orderId") String orderId) {
        return new Result<>(logisticsService.selectListByOrderId(orderId), "查询成功");
    }

    /**
     * 根据订单物流id获取订单物流信息
     *
     * @param id 订单物流id
     * @return 订单物流信息
     */
    @GetMapping("/{id}")
    @Auth(value = "clever-system.logistics.selectById", name = "根据订单物流id获取订单物流信息", description = "根据订单物流id获取订单物流信息接口")
    public Result<Logistics> selectById(@PathVariable("id") String id) {
        return new Result<>(logisticsService.selectById(id), "查询成功");
    }

    /**
     * 创建订单物流信息
     *
     * @param logistics 订单物流实体信息
     * @return 创建后的订单物流信息
     */
    @PostMapping("")
    @Auth(value = "order-second-hand-transaction.logistics.create", name = "创建订单物流", description = "创建订单物流信息接口")
    public Result<Logistics> create(@Validated Logistics logistics) {
        OnlineUser onlineUser = SpringUtil.getOnlineUser();
        return new Result<>(logisticsService.create(logistics, onlineUser), "创建成功");
    }

    /**
     * 修改订单物流信息
     *
     * @param logistics 订单物流实体信息
     * @return 修改后的订单物流信息
     */
    @PatchMapping("/{id}")
    @Auth(value = "order-second-hand-transaction.logistics.update", name = "修改订单物流", description = "修改订单物流信息接口")
    public Result<Logistics> update(@Validated Logistics logistics, @PathVariable("id") String id) {
        OnlineUser onlineUser = SpringUtil.getOnlineUser();
        logistics.setId(id);
        return new Result<>(logisticsService.update(logistics, onlineUser), "修改成功");
    }

    /**
     * 保存订单物流信息
     *
     * @param logistics 订单物流实体信息
     * @return 保存后的订单物流信息
     */
    @PostMapping("/save")
    @Auth(value = "order-second-hand-transaction.logistics.save", name = "保存订单物流", description = "保存订单物流信息接口")
    public Result<Logistics> save(@Validated Logistics logistics) {
        OnlineUser onlineUser = SpringUtil.getOnlineUser();
        return new Result<>(logisticsService.save(logistics, onlineUser), "保存成功");
    }

    /**
     * 根据订单物流id删除订单物流信息
     *
     * @param id 订单物流id
     */
    @DeleteMapping("/{id}")
    @Auth(value = "order-second-hand-transaction.logistics.delete", name = "删除订单物流", description = "删除订单物流信息接口")
    public Result<String> delete(@PathVariable("id") String id) {
        OnlineUser onlineUser = SpringUtil.getOnlineUser();
        logisticsService.delete(id, onlineUser);
        return Result.ofSuccess("删除成功");
    }
}
